package exam.Huawei414;

import java.util.Stack;

public class Ex1190 {
    public String reverseParentheses(String s) {
        Stack<Character> stack = new Stack<>();
        String res = "";

        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i)==')'){
                String temp = "";
                while (stack.peek()!='('){
                    temp = temp+stack.pop();
                }
                stack.pop();
                for (int j = 0; j < temp.length(); j++) {
                    stack.push(temp.charAt(j));
                }
            }else{
                stack.push(s.charAt(i));
            }
        }

        int n = stack.size();
        for (int i = 0; i < n; i++) {
            res = res+stack.pop();
        }
        return new StringBuffer(res).reverse().toString();
    }

    public static void main(String[] args) {
        Ex1190 ex1190 = new Ex1190();
        ex1190.reverseParentheses("(abcd)");

    }
}
